/*******************************************************************
This file is part of iLaunch.

iLaunch is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

iLaunch is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with iLaunch.  If not, see <http://www.gnu.org/licenses/>.
*******************************************************************/

#include "stdafx.h"
#include "FileUtils.h"

//// from phoneutils
//// dynamically loaded function
//typedef LONG (* TAPIREQUESTMAKECALL)(LPCTSTR, LPCTSTR, LPCTSTR, LPCTSTR);

#define MAX_COMMAND_LINE 200
#define MAX_LOADSTRING 100
// end phoneutils

// Thanks l3v5y!
bool GetIDialerFilename(TCHAR * szFilename) {

	HKEY hkey;

    // Where app install data is stored
	LONG result = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
        TEXT("Software\\Apps\\Supware.net iDialer"), 0, 
		KEY_QUERY_VALUE, &hkey);

    // Try to find iDialer from installation in registry
    if (0 == result) {
		DWORD buffersize = MAX_PATH;
		RegQueryValueEx(hkey, TEXT("InstallDir"), NULL, NULL,
            (PBYTE)szFilename, &buffersize);
		RegCloseKey(hkey);
	}

    // Otherwise, assume it's stored in the same location
    // (either both in memory or both on storage card)
	else {
        // generate path name for iLaunch
        ::GetModuleFileName(NULL, szFilename, MAX_PATH);
        TCHAR * pstr = _tcsrchr(szFilename, '\\');
        if (pstr) *(pstr) = '\0';

        // Go "up 1" directory. We should then be in \Program Files
        pstr = _tcsrchr(szFilename, '\\');
        if (pstr) *(++pstr) = '\0';

        // Concatenate iDialer program directory
        StringCchCat(szFilename, MAX_PATH, TEXT("iDialer"));
	}

    // Concatenate iDialer exe path
	StringCchCat(szFilename, MAX_PATH, TEXT("\\iDialer.exe"));

	return FileExists(szFilename);
}

bool FileExists(const TCHAR * szFilename) {
	// test if file exists
	HANDLE h = CreateFile(szFilename, GENERIC_READ,
		FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, NULL, NULL);
	bool exists = h != INVALID_HANDLE_VALUE;
	if (exists)
		CloseHandle(h);
	return exists;
}

void GetCurDirFilename(TCHAR * szFilename, const TCHAR * filename, 
                       const TCHAR * extension) {

    ::GetModuleFileName(NULL, szFilename, MAX_PATH);
    TCHAR * pstr = _tcsrchr(szFilename, '\\');
    if (pstr) *(++pstr) = '\0';
    StringCchCat(szFilename, MAX_PATH, filename);
    if (extension != NULL) {
        StringCchCat(szFilename, MAX_PATH, TEXT("."));
        StringCchCat(szFilename, MAX_PATH, extension);
    }
}


// from phoneutils - until the end
void OpenURL(const TCHAR * url) {
	SHELLEXECUTEINFO ShExecInfo = {0};

	ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
	ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
	ShExecInfo.hwnd = NULL;
	ShExecInfo.lpVerb = TEXT("open");
	ShExecInfo.lpFile = url;
	ShExecInfo.lpParameters = NULL;

	ShExecInfo.lpDirectory = NULL;
	ShExecInfo.nShow = SW_SHOW;
	ShExecInfo.hInstApp = NULL; 

	ShellExecuteEx(&ShExecInfo);
}
void RunDialer() {
	PROCESS_INFORMATION pi;

	TCHAR szDialerPath[MAX_PATH];

	if (GetIDialerFilename(szDialerPath)) {
		CreateProcess(szDialerPath, NULL,
			NULL, NULL, FALSE, NULL, NULL, NULL, NULL, &pi);
	}

	else {
		CreateProcess(TEXT("cprog.exe"), NULL, 
			NULL, NULL, FALSE, NULL, NULL, NULL, NULL, &pi);
	}
}

void RunTaskMgr() {
	PROCESS_INFORMATION pi;

	TCHAR szTaskMgrPath[MAX_PATH];

	if (GetITaskMgrFilename(szTaskMgrPath)) {
		CreateProcess(szTaskMgrPath, NULL,
			NULL, NULL, FALSE, NULL, NULL, NULL, NULL, &pi);
	}

	else {
		//CreateProcess(TEXT("cprog.exe"), NULL, 
		//	NULL, NULL, FALSE, NULL, NULL, NULL, NULL, &pi);
	}
}

bool GetITaskMgrFilename(TCHAR * szFilename) {

	HKEY hkey;

	// Where app install data is stored
	LONG result = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
		TEXT("Software\\iApps\\iTaskMgr"), 0, 
		KEY_QUERY_VALUE, &hkey);

	// Try to find iDialer from installation in registry
	if (0 == result) {
		DWORD buffersize = MAX_PATH;
		RegQueryValueEx(hkey, TEXT("InstallDir"), NULL, NULL,
			(PBYTE)szFilename, &buffersize);
		RegCloseKey(hkey);
	}

	// Otherwise, assume it's stored in the same location
	// (either both in memory or both on storage card)
	else {
		// generate path name for iLaunch
		::GetModuleFileName(NULL, szFilename, MAX_PATH);
		TCHAR * pstr = _tcsrchr(szFilename, '\\');
		if (pstr) *(pstr) = '\0';

		// Go "up 1" directory. We should then be in \Program Files
		pstr = _tcsrchr(szFilename, '\\');
		if (pstr) *(++pstr) = '\0';

		// Concatenate iDialer program directory
		StringCchCat(szFilename, MAX_PATH, TEXT("iTaskMgr"));
	}

	// Concatenate iDialer exe path
	StringCchCat(szFilename, MAX_PATH, TEXT("\\iTaskMgr.exe"));

	return FileExists(szFilename);
}
